import upload from './upload.js';
fetch("country-add.html").then(r => r.text()).then(text => {
	let addDialog = $(text);
	console.debug(addDialog);
	$("body").append(addDialog);

	let form = addDialog[0].querySelector("form");//form是原生标签元素,非jquery对象

	let btn = addDialog[0].querySelector("._save");

	console.debug(addDialog, form, btn);


	upload(addDialog.find("._upload")[0], function(data) {
		form.countryUri.value = data.uri;
	});
	btn.onclick = function() {
		let param = new URLSearchParams(new FormData(form));
		console.info(param.toString());
		fetch("country-add.let", {
			method: 'POST',
			mode: 'cors',
			cache: 'no-cache',
			credentials: 'include',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			redirect: 'follow',
			referrerPolicy: 'no-referrer', 
			body: param
		}).then(r => r.json()).then(data => {
			console.debug(data);
			if (data.ok) {
				addDialog.modal('hide');
				form.reset();
				window.postMessage("loadData");
			} else {
				alert(data.msg);
			}
		}).catch(e => {
			console.error(e);
		});
	}
});

export default function() {

}
